import router from './router'
import NProgress from 'nprogress' // Progress 进度条
import store from './vuex/store'
import 'nprogress/nprogress.css'// Progress 进度条样式
import { Message } from 'element-ui'
import { getToken } from '@/utils/auth' // 验权
import {
  setTitle
} from '@/utils/util'
import * as Cache from "./common/js/cache";
import {post} from "./common/js/http";
function judgePermission(token,from,to,next){//判断是否有权限
    if (token) {//登录了
        let tempI = -1
        let tempJ = -1
        let permissionRouters = store.getters.permission_routers
        for (let i in permissionRouters) {
            if (permissionRouters[i].children!=undefined) {
                for (let j in permissionRouters[i].children) {
                    if (to.name == permissionRouters[i].children[j]['name']) {
                        tempI = i;
                        tempJ = j;
                        break;
                    }
                }
            }else{
                if (to.name == permissionRouters[i]['name']) {
                    tempI = i;
                    break;
                }
            }
        }
        if (tempI==-1&&tempJ==-1){
            if (noPermissionList.indexOf(to.path) === -1) {
                if (whiteList.indexOf(to.path) === -1) {
                    Message({message: '对不起，没有权限访问该模块！', type: 'error'});
                    next({ path: '/401', query: { noGoBack: true }})
                }else{
                    next({ path: '/401', query: { noGoBack: true }})
                }
            }else{
                next()
            }
        } else{
            next()
        }
    }else {
        next({path: '/login'});
    }
    NProgress.done()
}
const whiteList = ['/login', '/lock'] // 不重定向白名单
const noPermissionList = ['/workPlace'] // 不判断权限
router.beforeEach((to, from, next) => {//在每个路由之前
    NProgress.start()
    if (getToken() && !to.query.noGoBack) {
        // 设置浏览器头部标题
        const browserHeaderTitle = to.meta.title
        store.commit('SET_BROWSERHEADERTITLE', {
            browserHeaderTitle: browserHeaderTitle
        })
        /* has token*/
        if (store.getters.isLock) {
            next({
                path: '/lock'
                ,query: { noGoBack: true }})
            NProgress.done()
        }else if (to.path === '/login') {
            next({ path: '/' })
            NProgress.done()
        } else {
            if (!navLoaded){
                post(
                    'basic-information-system-ygxx/find-navlist'
                    ,{userId: Cache.getCookie('gsid')
                    })
                    .then(res=>{
                        navLoaded = true
                        const roles = res.data.data // note: roles must be a array! such as: ['editor','develop']
                        store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
                            router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
                            judgePermission(getToken(),from,to,next)
                        })
                    })
            }else{
                judgePermission(getToken(),from,to,next)
            }
        }
    } else {
        // 设置浏览器头部标题
        const browserHeaderTitle = to.name
        store.commit('SET_BROWSERHEADERTITLE', {
            browserHeaderTitle: browserHeaderTitle
        })
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
            NProgress.done()
        }
    }
})
router.afterEach((to, from,next) => {
    NProgress.done() // 结束Progress
    setTimeout(() => {
        const browserHeaderTitle = store.getters.browserHeaderTitle
        setTitle(browserHeaderTitle)
        setTimeout(function () {//触发layui自动调节大小的事件
            var myEvent = new Event('resize');
            window.dispatchEvent(myEvent);
        },400)
    }, 0)
})